package com.hichao.cs.thrift.front;

import org.apache.thrift.TServiceClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class FrontClientProxyFactory {
	
	private int port;
	
	public void setPort(int port){
		this.port = port;
	}
	
	private String host;
	
	public void setHost(String host) {
		this.host = host;
	}
	
	class Client {

		private TServiceClient client;
		
		Client(){
		}
		
		TServiceClient open(){
			TSocket socket = 
					new TSocket(FrontClientProxyFactory.this.host, FrontClientProxyFactory.this.port);
			TTransport transport = new TFramedTransport(socket);
			TProtocol protocol = new TBinaryProtocol(transport);
			this.client = new FrontService.Client(protocol);
			return this.client;
		}
		
		void close(){
			TTransport inputTransport = this.client.getInputProtocol().getTransport();
			if (inputTransport != null && inputTransport.isOpen())
				try{
					inputTransport.close();
				}catch(Exception x){
					x.printStackTrace();
				}
			TTransport outputTransport = this.client.getOutputProtocol().getTransport();
			if (outputTransport != null && outputTransport.isOpen())
				try{
					outputTransport.close();
				}catch(Exception x){
					x.printStackTrace();
				}
		}

	}
	
}
